package com.neusoft.elm.dao.impl;
import java.util.ArrayList;
import java.util.List;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import com.neusoft.elm.dao.BusinessDao;
import com.neusoft.elm.po.Business;
import com.neusoft.elm.util.DBUtil;
public class BusinessDaoImpl implements BusinessDao{
 
 private Connection con = null;
 private PreparedStatement pst = null;
 private ResultSet rs = null;
 @Override
 public List<Business> listBusinessByOrderTypeId(Integer orderTypeId) throws Exception {
 List<Business> list = new ArrayList<>();
 String sql = "select * from business where orderTypeId=? order by businessId";
 try {
 con = DBUtil.getConnection();
 pst = con.prepareStatement(sql);
 pst.setInt(1, orderTypeId);
 rs = pst.executeQuery();
 while(rs.next()) {
 Business business = new Business();
 business.setBusinessId(rs.getInt("businessId"));
 business.setBusinessName(rs.getString("businessName"));
 business.setBusinessAddress(rs.getString("businessAddress"));
 business.setBusinessExplain(rs.getString("businessExplain"));
 business.setBusinessImg(rs.getString("businessImg"));
 business.setOrderTypeId(rs.getInt("orderTypeId"));
 business.setStarPrice(rs.getBigDecimal("starPrice"));
 business.setDeliveryPrice(rs.getBigDecimal("deliveryPrice"));
 business.setRemarks(rs.getString("remarks"));
 business.setBusinessUsername(rs.getString("businessUsername"));
 business.setBusinessPassword(rs.getString("businessPassword"));
 list.add(business);
 }
 }finally {
 DBUtil.close(rs,pst);
 }
 return list;
 }
 
 @Override
 public Business getBusinessById(Integer businessId) throws Exception{
 Business business = null;
 String sql = "select * from business where businessId=?";
 try {
 con = DBUtil.getConnection();
 pst = con.prepareStatement(sql);
 pst.setInt(1, businessId);
 rs = pst.executeQuery();
 while(rs.next()) {
 business = new Business();
 business.setBusinessId(rs.getInt("businessId"));
 business.setBusinessName(rs.getString("businessName"));
 business.setBusinessAddress(rs.getString("businessAddress"));
 business.setBusinessExplain(rs.getString("businessExplain"));
 business.setBusinessImg(rs.getString("businessImg"));
 business.setOrderTypeId(rs.getInt("orderTypeId"));
 business.setStarPrice(rs.getBigDecimal("starPrice"));
 business.setDeliveryPrice(rs.getBigDecimal("deliveryPrice"));
 business.setRemarks(rs.getString("remarks"));
 business.setBusinessUsername(rs.getString("businessUsername"));
 business.setBusinessPassword(rs.getString("businessPassword"));
 }
 }finally {
 DBUtil.close(rs,pst);
 }
 return business;
 }

 @Override
 public Business getBusinessByUsernamePassword(String username, String password) throws Exception {
 Business business = null;
 String sql = "select * from business where businessUsername=? and businessPassword=?";
 try {
 con = DBUtil.getConnection();
 pst = con.prepareStatement(sql);
 pst.setString(1, username);
 pst.setString(2, password);
 rs = pst.executeQuery();
 while(rs.next()) {
 business = new Business();
 business.setBusinessId(rs.getInt("businessId"));
 business.setBusinessName(rs.getString("businessName"));
 business.setBusinessAddress(rs.getString("businessAddress"));
 business.setBusinessExplain(rs.getString("businessExplain"));
 business.setBusinessImg(rs.getString("businessImg"));
 business.setOrderTypeId(rs.getInt("orderTypeId"));
 business.setStarPrice(rs.getBigDecimal("starPrice"));
 business.setDeliveryPrice(rs.getBigDecimal("deliveryPrice"));
 business.setRemarks(rs.getString("remarks"));
 business.setBusinessUsername(rs.getString("businessUsername"));
 business.setBusinessPassword(rs.getString("businessPassword"));
 }
 }finally {
 DBUtil.close(rs,pst);
 }
 return business;
 }

 @Override
 public int saveBusiness(Business business) throws Exception {
 int result = 0;
 String sql = "insert into business(businessName,businessAddress,businessExplain,businessImg,orderTypeId,starPrice,deliveryPrice,remarks,businessUsername,businessPassword) values(?,?,?,?,?,?,?,?,?,?)";
 try {
 con = DBUtil.getConnection();
 pst = con.prepareStatement(sql);
 pst.setString(1, business.getBusinessName());
 pst.setString(2, business.getBusinessAddress());
 pst.setString(3, business.getBusinessExplain());
 pst.setString(4, business.getBusinessImg());
 pst.setInt(5, business.getOrderTypeId());
 pst.setBigDecimal(6, business.getStarPrice());
 pst.setBigDecimal(7, business.getDeliveryPrice());
 pst.setString(8, business.getRemarks());
 pst.setString(9, business.getBusinessUsername());
 pst.setString(10, business.getBusinessPassword());
 result = pst.executeUpdate();
 }finally {
 DBUtil.close(pst);
 }
 return result;
 }
}
